package com.dormitory123.millet.dao.Impl;

import com.dormitory123.millet.dao.BaseDao;
import com.dormitory123.millet.dao.PhoneDao;
import com.dormitory123.millet.pojo.Phone;

import java.util.List;

/**
 * @author yanhuanzhan
 * @date 2022/1/4 - 16:53
 */
public class PhoneDaoImpl extends BaseDao implements PhoneDao {
    @Override
    public List<Phone> list() {
        String sql = "SELECT `id`,`name`,`price`,`discount_price` discountPrice,`sales`,`stock`,`img_path` imgPath,`describe`,`type` FROM t_phone";
        return queryForList(Phone.class, sql);
    }

    @Override
    public List<Phone> queryPhonesByType(String type) {
        String sql = "SELECT `id`,`name`,`price`,`discount_price` discountPrice,`sales`,`stock`,`img_path` imgPath,`describe`,`type` FROM t_phone WHERE type=?";
        return queryForList(Phone.class, sql,type);
    }

    @Override
    public Phone queryPhoneById(Integer id) {
        String sql = "SELECT `id`,`name`,`price`,`discount_price` discountPrice,`sales`,`stock`,`img_path` imgPath,`describe`,`type` FROM t_phone WHERE id=?";
        return queryForOne(Phone.class, sql, id);
    }

    @Override
    public int updatePhone(Phone phone) {
        String sql = "UPDATE t_phone SET `name`=?,`price`=?,`discount_price`=?,`sales`=?,`stock`=?,`img_path`=?,`describe`=?,`type`=? WHERE id=?";
        return update(sql, phone.getName(), phone.getPrice(), phone.getDiscountPrice(), phone.getSales(), phone.getStock(), phone.getImgPath(), phone.getDescribe(),phone.getType(),phone.getId());
    }
}
